//
//  KDDRegisterInfoStore.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//

#import "KDDConfigStore.h"

@interface KDDConfigStore()

@property (strong,nonatomic) NSMutableDictionary<NSString *,KDDConfig *> * configStore;

@property (strong,nonatomic) NSMutableArray<NSMutableDictionary *> * registerInfoStore;

@property (strong,nonatomic) NSMutableDictionary<NSString *,KDDRegisterInfo *> * headerStore;

@property (strong,nonatomic) NSMutableDictionary<NSString *,KDDRegisterInfo *> * cellStore;

@property (strong,nonatomic) NSMutableDictionary<NSString *,KDDRegisterInfo *> * footerStore;

@property (strong,nonatomic) NSMutableDictionary<NSString *,KDDRegisterInfo *> * decorationStore;

@end

@implementation KDDConfigStore

+ (KDDConfigStore *)sharedStore
{
    static KDDConfigStore * sharedStore = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedStore = [self new];
    });
    return sharedStore;
}

- (instancetype)init
{
    if (self = [super init])
    {
        self.configStore = [NSMutableDictionary dictionary];
        self.registerInfoStore = [NSMutableArray arrayWithCapacity:5];
        self.headerStore = [NSMutableDictionary dictionary];
        self.cellStore = [NSMutableDictionary dictionary];
        self.footerStore = [NSMutableDictionary dictionary];
        self.decorationStore = [NSMutableDictionary dictionary];
        [self.registerInfoStore addObject:[NSMutableDictionary dictionary]];
        [self.registerInfoStore addObject:self.headerStore];
        [self.registerInfoStore addObject:self.cellStore];
        [self.registerInfoStore addObject:self.footerStore];
        [self.registerInfoStore addObject:self.decorationStore];
    }
    return self;
}

- (void)addModelConfig:(KDDConfig *)config
{
    self.configStore[config.configID] = config;
}

- (KDDConfig *)modelConfigWithID:(NSString *)configID
{
    return self.configStore[configID];
}

- (void)removeModelConfig:(NSString *)configID
{
    self.configStore[configID] = nil;
}

- (void)addRegisterInfo:(KDDRegisterInfo *)info
{
    self.registerInfoStore[info.viewType][info.reuseID] = info;
}

- (KDDRegisterInfo *)registerInfoForReuseID:(NSString *)reuseID viewType:(KDDViewType)viewType
{
    return self.registerInfoStore[viewType][reuseID];
}

- (void)removeRegisterInfo:(NSString *)reuseID viewType:(KDDViewType)viewType
{
    [self.registerInfoStore[viewType] removeObjectForKey:reuseID];
}

- (void)registerDecorationView:(Class)cls insets:(UIEdgeInsets)insets zIndex:(NSInteger)zIndex reuseID:(NSString *)reuseID
{
    KDDRegisterInfo * registerInfo = [[KDDRegisterInfo alloc] initWithViewType:KDDViewTypeDecoration wrapperClass:cls reuseID:reuseID];
    registerInfo.insets = insets;
    registerInfo.zIndex = zIndex;
    [self addRegisterInfo:registerInfo];
}

@end

@implementation KDDConfigure
{
    NSMutableDictionary<NSString *,KDDRegisterInfo *> * _layoutRegisterDict;
}

- (instancetype)init
{
    if (self = [super init])
    {
        _configStore = [KDDConfigStore new];
        _layoutRegisterDict = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)applyToLayoutView:(UICollectionView *)layoutView
{
    _layoutView = layoutView;
    [_layoutRegisterDict removeAllObjects];
    for (NSMutableDictionary<NSString *,KDDRegisterInfo *> * store in self.configStore.registerInfoStore)
    {
        for (KDDRegisterInfo * info in store.allValues)
        {
            [self registerInfo:info];
        }
    }
}

- (void)registerDecorationView:(Class)cls insets:(UIEdgeInsets)insets zIndex:(NSInteger)zIndex reuseID:(NSString *)reuseID
{
    KDDRegisterInfo * registerInfo = [[KDDRegisterInfo alloc] initWithViewType:KDDViewTypeDecoration wrapperClass:cls reuseID:reuseID];
    registerInfo.insets = insets;
    registerInfo.zIndex = zIndex;
    [self addRegisterInfo:registerInfo];
}

- (void)addModelConfig:(KDDConfig *)config
{
    [self.configStore addModelConfig:config];
    for (KDDRegisterInfo * info in config.registerInfo.allValues)
    {
        [self addRegisterInfo:info];
    }
}

- (KDDConfig *)modelConfigWithID:(NSString *)configID
{
    return [self.configStore modelConfigWithID:configID];
}

- (KDDRegisterInfo *)registerInfoForReuseID:(NSString *)reuseID viewType:(KDDViewType)viewType
{
    if (self.layoutView != nil)
    {
        KDDRegisterInfo * registerInfo = _layoutRegisterDict[reuseID];
        if (registerInfo == nil)
        {
            registerInfo = [self.configStore registerInfoForReuseID:reuseID viewType:viewType];
            if (registerInfo != nil)
            {
                [self registerInfo:registerInfo];
            }
        }
        return registerInfo;
    }
    return [self.configStore registerInfoForReuseID:reuseID viewType:viewType];
}

- (void)addRegisterInfo:(KDDRegisterInfo *)info
{
    if (self.layoutView != nil)
    {
        [self registerInfo:info];
    }
    [self.configStore addRegisterInfo:info];
}

- (void)removeModelConfig:(NSString *)configID
{
    KDDConfig * config = self.configStore.configStore[configID];
    for (KDDRegisterInfo * info in config.registerInfo.allValues)
    {
        [self removeRegisterInfo:info.reuseID viewType:info.viewType];
    }
    [self.configStore removeModelConfig:configID];
}

- (void)registerInfo:(KDDRegisterInfo *)info
{
    if (info.viewType == KDDViewTypeCell)
    {
        [self.layoutView registerClass:info.wrapperClass forCellWithReuseIdentifier:info.reuseID];
    }
    else if (info.viewType == KDDViewTypeHeader)
    {
        [self.layoutView registerClass:info.wrapperClass forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:info.reuseID];
    }
    else if (info.viewType == KDDViewTypeFooter)
    {
        [self.layoutView registerClass:info.wrapperClass forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:info.reuseID];
    }
    else if (info.viewType == KDDViewTypeDecoration)
    {
        [self.layoutView.collectionViewLayout registerClass:info.wrapperClass forDecorationViewOfKind:info.reuseID];
    }
    _layoutRegisterDict[info.reuseID] = info;
}

- (void)removeRegisterInfo:(NSString *)reuseID viewType:(KDDViewType)viewType
{
    if (self.layoutView != nil)
    {
        if (viewType == KDDViewTypeCell)
        {
            [self.layoutView registerClass:nil forCellWithReuseIdentifier:reuseID];
        }
        else if (viewType == KDDViewTypeHeader)
        {
            [self.layoutView registerClass:nil forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseID];
        }
        else if (viewType == KDDViewTypeFooter)
        {
            [self.layoutView registerClass:nil forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseID];
        }
        else if (viewType == KDDViewTypeDecoration)
        {
            [self.layoutView.collectionViewLayout registerClass:nil forDecorationViewOfKind:reuseID];
        }
    }
    [self.configStore removeRegisterInfo:reuseID viewType:viewType];
    _layoutRegisterDict[reuseID] = nil;
}

@end
